package com.gitee.ricesugar.mall.model.Service;

import com.gitee.ricesugar.mall.model.DAO.UserDAO;
import com.gitee.ricesugar.mall.model.POJO.DO.UserDO;
import com.gitee.ricesugar.mall.model.POJO.DTO.LoginDTO;
import com.gitee.ricesugar.mall.model.POJO.DTO.RegisterDTO;

import java.sql.SQLException;

public class AccountService {

    private UserDAO userDAO;

    public AccountService() throws SQLException, ClassNotFoundException {
        userDAO = new UserDAO();
    }

    public RegisterDTO register(String username, String password) throws SQLException {
        UserDO userDO = null;
        userDO = userDAO.register(username, password);

        RegisterDTO registerDTO = null;

        if(userDO != null){
            registerDTO = new RegisterDTO(userDO.getUsername(), userDO.getPassword());
        }
        return registerDTO;
    }

    public LoginDTO login(String username, String password) throws SQLException {
        UserDO userDO = userDAO.login(username, password);
        LoginDTO loginDTO = null;
        if (userDO != null) {
            loginDTO =  new LoginDTO(userDO.getUsername(), userDO.getPassword());
        }
        return loginDTO;
    }


    public boolean isUsernameExist(String username) throws SQLException {
        return userDAO.isUsernameExist(username);
    }
}
